<template>
  <div class="construction-stats left-back">
    <div class="stats-container">
      <div class="chart-section">
        <div class="circle-container" v-if="constructionUnits.length > 0">
          <div class="circle-wrap" v-for="(unit, index) in constructionUnits" :key="unit.name">
            <svg class="circle-progress">
              <circle class="circle-bg" 
                     cx="100" 
                     cy="100" 
                     :r="getRadius(index)" 
                     :stroke="unit.color"
                     fill="none"
                     stroke-width="10"/>
              <circle class="circle" 
                     cx="100" 
                     cy="100" 
                     :r="getRadius(index)"
                     :stroke="unit.color"
                     fill="none"
                     stroke-width="10"
                     :style="getCircleStyle(unit.ratio, index)"/>
            </svg>
          </div>
        </div>
      </div>
      <div class="table-section">
        <table class="stats-table">
          <thead>
            <tr>
              <th>建设单位</th>
              <th>注册人数</th>
              <th>在场人数</th>
              <th>在场率</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="unit in constructionUnits" :key="unit.name">
              <td>
                <div class="unit-name">
                  <span class="dot" :style="{ backgroundColor: unit.color }"></span>
                  {{ unit.name }}
                </div>
              </td>
              <td>{{ unit.registered }}</td>
              <td>{{ unit.present }}</td>
              <td>{{ unit.ratio }}%</td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getProjectWorkerStatic, getSubcontractorWorkerStatic } from "@/js/api/businessApi.js"

const colors = ['#36D8D0', '#FFB340', '#4FCCFF']
const constructionUnits = ref([])

const getRadius = (index) => {
  // 从外到内递减半径
  return 90 - (index * 20)
}

const getCircleStyle = (ratio, index) => {
  const percentage = ratio / 100
  const circumference = 2 * Math.PI * getRadius(index)
  const offset = circumference * (1 - percentage)
  
  return {
    strokeDasharray: circumference + ' ' + circumference,
    strokeDashoffset: offset,
    transform: 'rotate(-90deg)',
    transformOrigin: '100px 100px'
  }
}

onMounted(() => {
  getSubcontractorWorkerStatic({"projectId": "19"}).then((data) => {
    constructionUnits.value = data.data.map((item, index) => {
      const ratio = Number(item.ratio)
      return {
        name: item.name,
        registered: Math.round(item.num / (ratio / 100)), // 根据在场率反推注册人数
        present: item.num,
        color: colors[index % colors.length], // 使用取模确保不会超出颜色数组范围
        ratio: ratio
      }
    })
  })
})
</script>

<style scoped>
.construction-stats {
  /* background: rgba(10, 25, 49, 0.8); */
  border-radius: 8px;
  padding: 20px;
  color: white;
}

.stats-container {
  display: flex;
  flex-direction: row;
  gap: 30px;
  align-items: center;
}

.chart-section {
  flex: 0 0 40%;
  position: relative;
  height: 300px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.table-section {
  flex: 1;
  min-width: 0; /* 防止表格溢出 */
}

.circle-container {
  position: relative;
  width: 100%;
  max-width: 250px;
  aspect-ratio: 1;
}

.circle-wrap {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.circle-progress {
  width: 100%;
  height: 100%;
  transform: rotate(0deg);
}

.circle-bg {
  opacity: 0.2;
}

.circle {
  transition: stroke-dashoffset 0.5s ease-in-out;
}

.stats-table {
  width: 100%;
  border-collapse: collapse;
  color: #fff;
}

.stats-table th {
  text-align: left;
  padding: 12px;
  font-weight: normal;
  color: #8F959E;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.stats-table td {
  padding: 12px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.unit-name {
  display: flex;
  align-items: center;
  gap: 8px;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

@media (max-width: 768px) {
  .stats-container {
    flex-direction: column;
    gap: 20px;
  }
  
  .chart-section {
    flex: none;
    width: 100%;
    height: 250px;
  }

  .circle-container {
    max-width: 200px;
  }
}
</style> 